package com.shijie.sjoj.service.core;

import com.shijie.sjoj.model.api.record.RecordService;
import com.shijie.sjoj.model.component.IdGeneratorRedis;
import com.shijie.sjoj.model.component.validate.NumberValidate;
import com.shijie.sjoj.model.exception.RecordException;
import com.shijie.sjoj.model.pojo.base.Status;
import com.shijie.sjoj.model.pojo.entity.Record;
import com.shijie.sjoj.model.utils.StringUtils;
import com.shijie.sjoj.service.mapper.RecordMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

/**
 * 解题记录实现类
 *
 * @author ZSJ
 */
@Slf4j
@DubboService
@RequiredArgsConstructor
public class RecordServiceImpl implements RecordService {

    private final RecordMapper recordMapper;

    private final IdGeneratorRedis idGenerator;

    @Override
    public List<Record> list(Long userId) {
        // 无效值校验
        if (NumberValidate.invalidSys(userId)) {
            throw RecordException.throwError("无效的用户id");
        }
        // 查询记录
        return recordMapper.list(userId);
    }

    @Override
    public Record getByRecordId(Long recordId) {
        // 无效值校验
        if (NumberValidate.invalidSys(recordId)) {
            throw RecordException.throwError("无效的记录id");
        }
        // 查询记录
        return recordMapper.getByRecordId(recordId);
    }

    @Override
    public boolean save(Record record) {
        // 无效值校验
        if (null == record) {
            throw RecordException.throwError("无效的记录");
        }
        if (NumberValidate.invalidSys(record.getUserId())) {
            throw RecordException.throwError("无效的用户id");
        }
        if (NumberValidate.invalidSys(record.getQuizId())) {
            throw RecordException.throwError("无效的题目id");
        }
        if (StringUtils.isBlank(record.getCode())) {
            throw RecordException.throwError("无效的代码");
        }
        if (null == record.getCodeType()) {
            throw RecordException.throwError("无效的代码类型");
        }
        // 记录格式化
        record.setRecordId(idGenerator.nextRecordId());
        record.setStatus(Status.Commited);
        record.setUseTimeMil(-1);
        record.setUseMemoryKb(-1);
        record.setErrorTip("");
        // 新增记录
        return recordMapper.save(record);
    }

    @Override
    public boolean changeStatus(Long recordId, Status status) {
        // 无效值校验
        if (NumberValidate.invalidSys(recordId)) {
            throw RecordException.throwError("无效的记录id");
        }
        if (null == status) {
            throw RecordException.throwError("无效的状态");
        }
        // 变更状态
        return recordMapper.changeStatus(recordId, status);
    }

    @Override
    public boolean errorInfo(Long recordId, String errorTip) {
        // 无效值校验
        if (NumberValidate.invalidSys(recordId)) {
            throw RecordException.throwError("无效的记录id");
        }
        if (StringUtils.isBlank(errorTip)) {
            throw RecordException.throwError("无效的错误提示");
        }
        // 记录错误提示
        return recordMapper.errorInfo(recordId, errorTip);
    }

    @Override
    public boolean successInfo(Long recordId, Integer userTimeMil, Integer useMemoryKb) {
        // 无效值校验
        if (NumberValidate.invalidSys(recordId)) {
            throw RecordException.throwError("无效的记录id");
        }
        if (NumberValidate.invalidZero(userTimeMil)) {
            throw RecordException.throwError("无效的使用时间");
        }
        if (NumberValidate.invalidZero(useMemoryKb)) {
            throw RecordException.throwError("无效的使用内存");
        }
        // 记录使用时间和使用内存
        return recordMapper.successInfo(recordId, userTimeMil, useMemoryKb);
    }


}
